package de.easywakeup.gui;

import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;
import de.easywakeup.R;
import de.easywakeup.appService.PersistenceHandler;
import de.easywakeup.appService.TimerHandler;
import de.easywakeup.data.AlarmList;
import de.easywakeup.data.Data;

public class AlarmSettings extends Activity

{

	/** Private members of the class */
	private Data data;

	private TextView alarmTime;
	private EditText nameOfAlarmEdit;
	private ToggleButton bt_mo;
	private ToggleButton bt_di;
	private ToggleButton bt_mi;
	private ToggleButton bt_do;
	private ToggleButton bt_fr;
	private ToggleButton bt_sa;
	private ToggleButton bt_so;
	private CheckBox checkBoxCalender;
	private CheckBox checkBoxWeather;
	private SeekBar seekBar;
	private int chooseVolumeOfSound;
	private CheckBox checkBoxVibration;
	private CheckBox checkBoxAlarmInSpeech;

	private boolean alarmEmpty;

	private int pHour;
	private int pMinute;

	private AlarmList al_temp;

	private String extraFlag;

	private Intent intent;

	// This integer will uniquely define the dialog to be used for displaying
	// time picker.
	static final int TIME_DIALOG_ID = 0;

	public AlarmSettings( )
	{
		data = Data.singleton( );
		data.setAlarmSettings( this );
	}

	/** Called when the activity is first created. */
	@SuppressLint( "NewApi" )
	@Override
	public void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_alarm_settings );

		// Set Data
		data = Data.singleton( );

		// Intent to choose the musik
		intent = new Intent( this, ChooseMusikAlert.class );

		/** Capture our View elements */
		alarmTime = (TextView) findViewById( R.id.alarmTime );
		nameOfAlarmEdit = (EditText) findViewById( R.id.NameOfAlarmEdit );
		bt_mo = (ToggleButton) findViewById( R.id.buttonMo );
		bt_di = (ToggleButton) findViewById( R.id.buttonDi );
		bt_mi = (ToggleButton) findViewById( R.id.buttonMi );
		bt_do = (ToggleButton) findViewById( R.id.buttonDo );
		bt_fr = (ToggleButton) findViewById( R.id.buttonFr );
		bt_sa = (ToggleButton) findViewById( R.id.buttonSa );
		bt_so = (ToggleButton) findViewById( R.id.buttonSo );
		checkBoxCalender = (CheckBox) findViewById( R.id.CheckBoxCalender );
		checkBoxWeather = (CheckBox) findViewById( R.id.CheckBoxWeather );
		seekBar = (SeekBar) findViewById( R.id.ChooseVolumeOfSound );
		checkBoxVibration = (CheckBox) findViewById( R.id.CheckBoxVibration );
		checkBoxAlarmInSpeech = (CheckBox) findViewById( R.id.CheckBoxAlarmInSpeech );

		alarmEmpty = false;

		/** Listener for click event of the button */
		alarmTime.setOnClickListener( new View.OnClickListener( )
		{
			@Override
			public void onClick( View v )
			{
				showDialog( TIME_DIALOG_ID );
			}
		} );

		seekBar.setOnSeekBarChangeListener( new OnSeekBarChangeListener( )
		{

			@Override
			public void onStopTrackingTouch( SeekBar seekBar )
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch( SeekBar seekBar )
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser )
			{
				chooseVolumeOfSound = progress;

			}
		} );

		/** Get the current time */
		final Calendar cal = Calendar.getInstance( );

		/** Display the current time in the TextView */
		extraFlag = getIntent( ).getStringExtra( "AlarmName" );

		// if is a new alert, it will create a new element. Otherwise it load
		// the old settings in the GUI
		if( extraFlag.equals( "null" ) )
		{

			al_temp = new AlarmList( );
			pHour = cal.get( Calendar.HOUR_OF_DAY );
			pMinute = cal.get( Calendar.MINUTE );

			updateDisplay( );
			al_temp.setAlarmTime( new StringBuilder( ).append( pad( pHour ) ).append( ":" ).append( pad( pMinute ) ).toString( ) );

			extraFlag = al_temp.getAlarmTime( );
			data.getAlarmList( ).add( al_temp );
			alarmEmpty = true;
		}
		else
		{

			al_temp = data.getAlarmListByTime( extraFlag );
			StringBuilder sb = new StringBuilder( );
			sb.append( pad( al_temp.getAlarmTimeHour( ) ) );
			sb.append( ":" );
			sb.append( pad( al_temp.getAlarmTimeMinute( ) ) );
			String text = sb.toString( );
			alarmTime.setText( text );
			pHour = al_temp.getAlarmTimeHour( );
			pMinute = al_temp.getAlarmTimeMinute( );

			nameOfAlarmEdit.setText( al_temp.getNameOfAlarmEdit( ) );
			bt_mo.setChecked( al_temp.isBt_moClicked( ) );
			bt_di.setChecked( al_temp.isBt_diClicked( ) );
			bt_mi.setChecked( al_temp.isBt_miClicked( ) );
			bt_do.setChecked( al_temp.isBt_doClicked( ) );
			bt_fr.setChecked( al_temp.isBt_frClicked( ) );
			bt_sa.setChecked( al_temp.isBt_saClicked( ) );
			bt_so.setChecked( al_temp.isBt_soClicked( ) );
			checkBoxCalender.setChecked( al_temp.isCheckBoxCalenderClicked( ) );
			checkBoxWeather.setChecked( al_temp.isCheckBoxWeatherClicked( ) );
			checkBoxVibration.setChecked( al_temp.isCheckBoxVibrationClicked( ) );
			checkBoxAlarmInSpeech.setChecked( al_temp.isCheckBoxAlarmInSpeechClicked( ) );

			seekBar.setProgress( al_temp.getChooseNoiseOfSound( ) );

		}

	}

	@Override
	public void onBackPressed( )
	{
		if( alarmEmpty )
		{
			data.deleteAlarmListByTime( extraFlag );
		}

		Intent intent = new Intent( this, MainScreen.class );
		startActivity( intent );

	}

	public void chooseMusikButtonListener( View v )
	{
		// Generate Intent in onCreate Method
		intent.putExtra( "Alarm", extraFlag );
		startActivity( intent );

	}

	public void chooseVolumeButtonListener( View v )
	{

	}

	public void declineButtonListener( View v )
	{
		data.deleteAlarmListByTime( extraFlag );
		TimerHandler.stopAlarm( al_temp );
		// SaveData
		PersistenceHandler per = new PersistenceHandler( this, data );
		per.saveData( );

		onBackPressed( );
	}

	public void okButtonListener( View v )
	{
		updateData( );

		// Set default values;
		al_temp.setAlreadyOpenedAlert( false );
		al_temp.setAlreadyPlayingMusik( false );

		// SaveData
		PersistenceHandler per = new PersistenceHandler( this, data );
		per.saveData( );

		// Set Timer for Alarm
		al_temp = TimerHandler.setTimer( this, al_temp, false, 0 );

		Intent intent = new Intent( this, MainScreen.class );
		startActivity( intent );

	}

	public void updateData( )
	{
		al_temp.setAlarmTimeHour( pHour );
		al_temp.setAlarmTimeMinute( pMinute );
		al_temp.setAlarmTime( new StringBuilder( ).append( pad( pHour ) ).append( ":" ).append( pad( pMinute ) ).toString( ) );
		al_temp.setNameOfAlarmEdit( nameOfAlarmEdit.getText( ).toString( ) );
		al_temp.setBt_moClicked( bt_mo.isChecked( ) );
		al_temp.setBt_diClicked( bt_di.isChecked( ) );
		al_temp.setBt_miClicked( bt_mi.isChecked( ) );
		al_temp.setBt_doClicked( bt_do.isChecked( ) );
		al_temp.setBt_frClicked( bt_fr.isChecked( ) );
		al_temp.setBt_saClicked( bt_sa.isChecked( ) );
		al_temp.setBt_soClicked( bt_so.isChecked( ) );
		al_temp.setCheckBoxCalenderClicked( checkBoxCalender.isChecked( ) );
		al_temp.setCheckBoxWeatherClicked( checkBoxWeather.isChecked( ) );
		al_temp.setCheckBoxVibrationClicked( checkBoxVibration.isChecked( ) );
		al_temp.setCheckBoxAlarmInSpeechClicked( checkBoxAlarmInSpeech.isChecked( ) );
		// Set Musik
		al_temp.setChoosedSound( data.getTemp_chooseMusik( ) );
		// Set Volume
		al_temp.setChooseNoiseOfSound( chooseVolumeOfSound );
		data.setAlarmListByTime( extraFlag, al_temp );
	}

	// START: DIALOG
	/** Create a new dialog for time picker */

	@Override
	protected Dialog onCreateDialog( int id )
	{
		switch( id)
		{
			case TIME_DIALOG_ID:
				return new TimePickerDialog( this, mTimeSetListener, pHour, pMinute, false );
		}
		return null;
	}

	/** Callback received when the user "picks" a time in the dialog */
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener( )
	{
		@Override
		public void onTimeSet( TimePicker view, int hourOfDay, int minute )
		{
			pHour = hourOfDay;
			pMinute = minute;
			/** Updates the time in the TextView */
			updateDisplay( );
		}
	};

	private void updateDisplay( )
	{
		alarmTime.setText( new StringBuilder( ).append( pad( pHour ) ).append( ":" ).append( pad( pMinute ) ) );
	}

	/** Add padding to numbers less than ten */
	public static String pad( int c )
	{
		if( c >= 10 )
			return String.valueOf( c );
		else
			return "0" + String.valueOf( c );
	}

	// END: DIALOG

	@Override
	public void onSaveInstanceState( Bundle savedInstanceState )
	{
		super.onSaveInstanceState( savedInstanceState );
		// Save UI state changes to the savedInstanceState.
		// This bundle will be passed to onCreate if the process is
		// killed and restarted.
		savedInstanceState.putString( "AlarmName", extraFlag );

		// etc.
	}

	@Override
	public void onRestoreInstanceState( Bundle savedInstanceState )
	{
		super.onRestoreInstanceState( savedInstanceState );
		// Restore UI state from the savedInstanceState.
		// This bundle has also been passed to onCreate.
		extraFlag = savedInstanceState.getString( "AlarmName" );
	}

}